package com.xjm.api.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjm.entity.model.ClassifyModel;
import com.xjm.entity.vo.PageVo;
import com.xjm.entity.vo.Result;
import com.xjm.service.ClassifyService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/classify")
@RequiredArgsConstructor
public class ClassifyController {

    private final ClassifyService classifyService;

    @GetMapping
    public Result<PageVo<ClassifyModel>> list(
            @RequestParam(defaultValue = "1") Long page,
            @RequestParam(defaultValue = "5") Long size
    ) {
        return Result.success(classifyService.page(new Page<>(page, size)));
    }

    @GetMapping("/{id}")
    public Result<ClassifyModel> getById(@PathVariable Long id) {
        return Result.success(classifyService.getById(id));
    }

    @PostMapping
    public Result<ClassifyModel> add(@RequestBody ClassifyModel classifyModel) {
        classifyService.save(classifyModel);
        return Result.success();
    }

    @PutMapping
    public Result<ClassifyModel> update(@RequestBody ClassifyModel classifyModel) {
        classifyModel.setUpdatedTime(null);
        classifyService.updateById(classifyModel);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<ClassifyModel> delete(@PathVariable Long id) {
        classifyService.removeById(id);
        return Result.success();
    }
}
